/***************************************************************************
 *   Copyright (c) 2008 Jürgen Riegel <juergen.riegel@web.de>              *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/


#include "pch.h"
#include <boost_signals2.hpp>

#include <Base/Tools.h>


#include "Sketcher/App/Constraint.h"

#include <QDateTime>

#include "Base/BaseClass.h"
#include <boost/uuid/uuid_generators.hpp>

using namespace Sketcher;
using namespace Base;


TYPESYSTEM_SOURCE(Sketcher::Constraint, Base::BaseClass)

Constraint::Constraint()
    : Value(0.0),
      Type(None),
      AlignmentType(Undef),
      Name(""),
      First(GeoUndef),
      FirstPos(PointPos::none),
      Second(GeoUndef),
      SecondPos(PointPos::none),
      Third(GeoUndef),
      ThirdPos(PointPos::none),
      LabelDistance(10.f),
      LabelPosition(0.f),
      isDriving(true),
      InternalAlignmentIndex(-1),
      isInVirtualSpace(false),
      isActive(true) {
    // Initialize a random number generator, to avoid Valgrind false positives.
    static boost::mt19937 ran;
    static bool seeded = false;

    if (!seeded) {
        ran.seed(QDateTime::currentMSecsSinceEpoch() & 0xffffffff);
        seeded = true;
    }
    static boost::uuids::basic_random_generator<boost::mt19937> gen(&ran);

    tag = gen();
}

Constraint* Constraint::clone(void) const {
    return new Constraint(*this);
}

Constraint* Constraint::copy(void) const {
    auto temp = new Constraint();
    temp->Value = this->Value;
    temp->Type = this->Type;
    temp->AlignmentType = this->AlignmentType;
    temp->Name = this->Name;
    temp->First = this->First;
    temp->FirstPos = this->FirstPos;
    temp->Second = this->Second;
    temp->SecondPos = this->SecondPos;
    temp->Third = this->Third;
    temp->ThirdPos = this->ThirdPos;
    temp->LabelDistance = this->LabelDistance;
    temp->LabelPosition = this->LabelPosition;
    temp->isDriving = this->isDriving;
    temp->InternalAlignmentIndex = this->InternalAlignmentIndex;
    temp->isInVirtualSpace = this->isInVirtualSpace;
    temp->isActive = this->isActive;
    // Do not copy tag, otherwise it is considered a clone, and a "rename" by the expression engine.
    return temp;
}


unsigned int Constraint::getMemSize(void) const {
    return 0;
}

void Constraint::substituteIndex(int fromGeoId, int toGeoId) {
    if (this->First == fromGeoId) {
        this->First = toGeoId;
    }
    if (this->Second == fromGeoId) {
        this->Second = toGeoId;
    }
    if (this->Third == fromGeoId) {
        this->Third = toGeoId;
    }
}
